package com.bysc.commonutils.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * 分页结果封装对象
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageResult implements Serializable{
    private Long total;//总记录数
    private Integer size;//当前页记录数
    private Long pages;//总页数
    private Long current;//当前页数
    private List records;//当前页结果

    public PageResult(Long total, int size, List records, Integer page) {
        this.total = total;
        this.size = size;
        this.records = records;
        this.pages = getPages();
        this.current = getCurrent(page);
    }
    public long getPages() {
        if (this.getSize() == 0L) {
            return 0L;
        } else {
            long pages = this.getTotal() / this.getSize();
            if (this.getTotal() % this.getSize() != 0L) {
                ++pages;
            }
            return pages;
        }
    }

    private long getCurrent(Integer page) {
        long pages = this.pages;
        if (this.size == 0L) {
            return 0L;
        } else {
            long current = page;
            if (page > pages) {
                current = pages;
            }
            return current;
        }
    }

}
